package com.bdqn.web.contoller;

import bean.User;
import com.bdqn.common.utils.EncryptUtil;
import com.bdqn.service.UserSevice;
import com.bdqn.service.utils.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/app/member")
public class Usercontoller {

    @Autowired
    private UserSevice userSevice;

    @Autowired
    Email Email;

    //获取后端工具类传递过来的随机验证码
    static String verifyCode  = null;

    @CrossOrigin(origins = "*",maxAge = 3600)
    @RequestMapping(value = "/login")
    public int getUser(@RequestParam(name = "phone") String username,@RequestParam(name = "password") String password) {
        User login_user = userSevice.Login_find(username,password);
        if(login_user != null){
            return 1;
        }
        return 0;
    }

    @CrossOrigin(origins = "*",maxAge = 3600)
    @RequestMapping(value = "/send/result")
    public Map<Object,Object> token(@RequestParam("phone") String username, @RequestParam("password") String password) throws JSONException {
        Map<Object,Object> map = userSevice.find(username, password);
        System.out.println(map);
        return map;
    }

    /**
     * 注册contoller
     * @param phone
     * @param password
     * @param Codes
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public int getregister(@RequestParam(value = "phone",required = false) String phone, @RequestParam String password,@RequestParam String Codes,@RequestParam String invitation,@RequestParam String email) {
        //将传入的验证码变成大写
        String code= Codes;
        String getcode  = code.toUpperCase();

        String QuEmail = userSevice.QueryEmail(email);
        Integer phone1 = userSevice.phone(phone);
        String invitationcode = userSevice.Invitationcode(invitation);
        if(getcode.equals(verifyCode) ){
            if(phone1 != null){
                return 3;
            }
            if (QuEmail != null){
                return 4;
            }
            if(invitationcode == null) {
                return 2;
            }
            userSevice.register(phone, password,getcode,email);
            return 1;

        }else{
            return 0;
        }
    }

    /**
     *邮箱验证码发送
     * @param email
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/email", method = RequestMethod.POST)
    public boolean Email(@RequestParam String email) throws MessagingException {
        return Email.getEmail(email,Code());
    }

    /**
     *忘记密码邮箱验证码发送
     * @param phone
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/forgetEMAIL",method=RequestMethod.POST)
    public Integer ForgetEmail(@RequestParam("phone") String phone) throws MessagingException {
        User user = userSevice.select_find(phone);
        if(user == null){
            return 0;
        }
        return Email.forgetEmail(userSevice.QueryEmail(phone),Code());
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/forgetpwd", method = RequestMethod.POST)
    public Integer forgetPwd(@RequestParam("Codes")String Codes,@RequestParam String password,@RequestParam String phone) throws MessagingException {
        String code= Codes;
        String getcode  = code.toUpperCase();
        if(getcode.equals(verifyCode)){
            Integer forget = userSevice.forget(password, phone);
            if(forget == null){
                return 0;
            }
            return 1;
        }else {
            return 0;
        }
    }

    public static  String Code(){
        String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb=new StringBuilder(4);
        for(int i=0;i<4;i++)
        {
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        verifyCode = sb.toString();
        return sb.toString();

    }


//    /**
//     * 注册图形验证码Contoller
//     * @param request
//     * @param response
//     * @throws IOException
//     */
//    @GetMapping(value = "/imgs")
//    public void authImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setHeader("Pragma", "No-cache");
//        response.setHeader("Cache-Control", "no-cache");
//        response.setDateHeader("Expires", 0);
//        response.setContentType("image/jpeg");
//        // 存入会话session
//        HttpSession session = request.getSession();
//        // 删除以前的
//        session.removeAttribute("verCode");
//        session.removeAttribute("codeTime");
//        // 生成随机大写字串
//        verifyCode = ImgCode.generateVerifyCode(4);
//        System.out.println(verifyCode);
//        session.setAttribute("verCode", verifyCode.toLowerCase());
//        session.setAttribute("codeTime", new Date());
//        // 生成图片
//        int w = 100, h = 30;
//        OutputStream out = response.getOutputStream();
//        ImgCode.outputImage(w, h, out, verifyCode);
//     }
}
